// Hal Pawluk 1999-2002   http://www.pawluk.com/public/

	var pageDOM="";
	var Myscript = "";
	var itsHere = false;		// tests for script in page
	var itsThere = false;	// tests for script in frameset
	var thisFrameset = "";
	var thisFrame = "";
	var activePage="";

	function isItHere(){		// called by onLoad in body tag
		checkForDWfile();
		pageDOM = dreamweaver.getDocumentDOM("document");
		activePage = fileOnly(pageDOM.URL);
		var theHeadNode = pageDOM.getElementsByTagName("HEAD");
		if (theHeadNode.item(0).innerHTML.toLowerCase().indexOf(end.toLowerCase()) != -1){

			//--	look up frameset and frame names specified
			var allScripts = pageDOM.getElementsByTagName("SCRIPT");
			var jamScript = "";
			if (allScripts) {
				for (i=0; i<allScripts.length; i++) {
					if (allScripts[i].innerHTML.toLowerCase().indexOf(end.toLowerCase()) != -1){
						jamScript = allScripts[i].innerHTML;
						break;
				}	}	}			// end if-allscripts
			jamScript = jamScript.substring(jamScript.indexOf('replace')+9);
			var theSplit = jamScript.indexOf('?');
			thisFrameset = jamScript.substring(0,theSplit);
			jamScript = jamScript.substring(jamScript.lastIndexOf('~'));
			thisFrame = jamScript.substring(1,jamScript.indexOf("'"));
			//-- end of names lookup

			theChoices = document.entryForm.getFrame
			for (var i=theChoices.options.length-1; i > 0 ;i--)		// reduce list to one item
				theChoices.options[i] = null;
			theChoices.options[0].text = thisFrame;
			
			document.entryForm.theFrameset.options[0].text = thisFrameset;
			itsHere = true;
		}else{
			thisFrameset = '';
			thisFrame = '';
			}
		return itsHere;
		}								// end isItHere ---

	var jamFrameset = "";
	var jamFramesetHome = "";
	var jamFrame ="";
	
	function insertScript(){	// insert in page and handler in frameset
		if (itsHere){
			openDocRemove();
			getFramesetHome();	
			}
		jamFrameset = document.entryForm.theFrameset.options[0].text;
		theChoices = document.entryForm.getFrame
	
		if (jamFrameset=='Pick a target frameset for this page'){
			alert('Please pick a target frameset.');
			return;
		}else if (theChoices.selectedIndex==0 && theChoices.options.length>1){
			alert('Please specify a frame for this page.');
			return;
		}else{						//we're good to go
			jamFrame = theChoices.options[theChoices.selectedIndex].text;
			if (jamFrame.indexOf("_") != -1)
				jamFrame = jamFrame.substring(0,jamFrame.indexOf("_"));

			Myscript = "\r"+ start + thisVersion +"\r"
			+ "if (window.name!='"+ jamFrame +"' && window.name!='booker_'\r"
			+ "		 && !((self.innerHeight == 0) && (self.innerWidth == 0)))\r"
			//next line was great but doesn't work in Netscape 4.08
			//+ "		top.location.replace('"+ jamFrameset +"?'+ location.href +'~"+ jamFrame +"');\r"
			
			// activePage is updated in isItHere() and getFramesetHome()
			+ "		top.location.replace('"+ jamFrameset +"?"+  activePage   +"~"+ jamFrame +"');\r"

			+ end + "\r"
			openDocInsert();		// script into page

			// Now open the frameset target of the 'frameJammer' script above, write a function
			// to handle 'jammed' pages, then save the frameset page.
			
			Myscript = "\r"+ start + thisVersion +"\r"
			+ "if (self != top)     // frame buster, may be removed\r"
			+ "	top.location.replace(self.location);\r\r"
			+ "function " + thisFnName + "(){\r"
			+ "	var framedPage = location.search;\r"
			+ "	if (framedPage.length > 1 && framedPage.indexOf(\"://\") == -1){\r"
			+ "		framedPage = framedPage.substring(1);\r"
			+ "		var theSplit = framedPage.lastIndexOf('~');\r"
			+ "		var thePage = framedPage.substring(0,theSplit);\r"
/*			
			+ "	if (thePage.indexOf('@') !=-1){  // checks for anchor\r"
			+ "		re=/(\\S+)@(\\S+)/\r"
			+ "		theArray = re.exec(thePage);\r"
			+ "		thePage=theArray[1]+'#'+theArray[2];\r"
			+ "		}\r"
*/
			+ "	// insert anchor code here if required (see Help)\r"
			+ "		var theFrame = framedPage.substring(theSplit+1);\r"
			+ "		eval(\"top.\"+theFrame+\".location.replace('\"+ thePage+\"')\");}\r"
			+ "	}\r\r"
			+ end + "\r";
			
			pageDOM = dreamweaver.getDocumentDOM(jamFramesetHome);
			//pageDOM.body.setAttribute('onLoad','frameJammer_hp()');
			closedDocInsert(pageDOM);
			if (!itsThere)
				pageDOM.body.setAttribute('onLoad','frameJammer_hp()');
			dreamweaver.saveDocument(pageDOM,pageDOM.URL);
			window.close();
			}							// end good to go
		}  							// end insertScript

	function getFramesetHome(){		// gets path to frameset, confirms it exists
		// called by browseFile and insertScript, 'jamFramesetHome' used by checkFrames
		activePage = fileOnly(pageDOM.URL);		
		var pathToPage = pageDOM.URL;
		pathToPage = pathToPage.substring(0,pathToPage.lastIndexOf("/"));
		jamFramesetHome = noSpaces(document.entryForm.theFrameset.options[0].text);
		var preFix="";
		while (jamFramesetHome.charAt(0) == "."){
			jamFramesetHome = jamFramesetHome.substring(jamFramesetHome.indexOf("/")+1);
			preFix = pathToPage.substring(pathToPage.lastIndexOf("/")) + preFix;
			pathToPage = pathToPage.substring(0,pathToPage.lastIndexOf("/"));	
			}
		jamFramesetHome = pathToPage +"/"+ jamFramesetHome;

		// added two lines
		preFix = preFix.charAt(0)=='/'? preFix.substring(1)+"/" : preFix;
		activePage = preFix + activePage;

		if ( !DWfile.exists( jamFramesetHome ) ){
			alert("Sorry, can't find the file '"+ jamFrameset+"'");
			return false;
		}else 
			return true;
		}


	var isDefault = -1;
	function checkFrames(){
		theChoices = document.entryForm.getFrame
		for (var i=theChoices.options.length-1; i > 0 ;i--)
			theChoices.options[i] = null;
		var framesDOM = dreamweaver.getDocumentDOM(jamFramesetHome);
		theFrames = framesDOM.getElementsByTagName('FRAME')
		if (theFrames.length == 0){
			alert('Sorry, can\'t find any frames in this page');
			document.entryForm.theFrameset.options[0].text = "Pick a target frameset for this page";
			theChoices.options[0].text = '--- Frame names will be listed here ---';
		}else{
			theChoices.options[0].text = 'Now pick a frame for this page:';
			var theCut = pageDOM.URL.lastIndexOf('/') + 1;
			var thisPage = pageDOM.URL.substring(theCut);
			isDefault = -1;
			for (var i=0; i< theFrames.length; i++){
				if (theFrames[i].getAttribute('SRC').indexOf(thisPage) != -1){
					isDefault = i;
					break;
			}	}	// end else theFrames.length
			if (isDefault >= 0){
				theChoices.options[0].text = theFrames[isDefault].getAttribute('NAME');
				theChoices.options[0].selected = true;
			}else{
				for (var i=0; i< theFrames.length; i++)
					theChoices.options[i+1] = new Option ( theFrames[i].getAttribute('NAME') );
					theChoices.options[0].selected = true;
			}	}	//end isDefault
		document.entryForm.theFrameset.options[0].selected = true; // 'prints' frameset name
		}	//end checkFrames


	function removeScript(){
		if (itsHere){
			openDocRemove();
			window.close();
		}else{
			alert(thisFnName + ' is not in this page.');
			return;
		}	}

	//---------------------
	
function openDocInsert(){
    theLocalDom=dreamweaver.getDocumentDOM("document");
    scriptinsert(theLocalDom);
	}
	
function openDocRemove(){
    theLocalDom=dreamweaver.getDocumentDOM("document");
    scriptdelete(theLocalDom);
	}

function closedDocInsert(otherDom){
    scriptinsert(otherDom);
	}
	
function closedDocRemove(otherDom){
    scriptdelete(otherDom);
	}

function scriptinsert(thePageDom){
	theHeadNode=thePageDom.getElementsByTagName("HEAD");
	itsThere=theHeadNode.item(0).innerHTML.toLowerCase().indexOf(codeToCheck.toLowerCase())!="-1";
	if (itsThere);
	else
  		theHeadNode.item(0).innerHTML += '<script language="javascript" type="text/javascript">' + Myscript + '</s'+'cript>';
	}

function scriptdelete(thePageDom){
	theHeadNode = thePageDom.getElementsByTagName("HEAD");
	itsThere = theHeadNode.item(0).innerHTML.toLowerCase().indexOf(codeToCheck.toLowerCase()) != -1;	
	if(itsThere){
		theScriptNodes=thePageDom.getElementsByTagName("SCRIPT");
		for(var i=0;i<theScriptNodes.length;i++){
			var innerScript = theScriptNodes[i].innerHTML.toLowerCase();
			if (innerScript.indexOf(codeToCheck.toLowerCase()) != -1){
				var beginsAt = theHeadNode.item(0).innerHTML.toLowerCase().indexOf(start.toLowerCase());
				var endsAt   = theHeadNode.item(0).innerHTML.toLowerCase().indexOf(end.toLowerCase());
				var theChunk = theHeadNode.item(0).innerHTML.substring(beginsAt,endsAt+end.length);
				theHeadNode.item(0).innerHTML = subtractString(theHeadNode.item(0).innerHTML, theChunk);
				var leavings = noSpaces(theScriptNodes[i].innerHTML);
				if (leavings=="<!--//-->")		// need to separate the tests or DW breaks
					theScriptNodes[i].outerHTML = "" ;
				else if (leavings=="")			// removes empty script tags
					theScriptNodes[i].outerHTML = "" ;
				break;
				}
			}
		}
	}

// ---- support functions ----

function noSpaces(theString){
	var theEnd = theString.length;
	for (var i=0; i< theEnd; i++){
		while ( theString.charCodeAt(i)<33 && i<theEnd){
			theString = theString.substring(0,i) + theString.substring(i+1);
			theEnd-- ;
			}
		}
	return theString;
	}
	
function subtractString(theString,theTakeaway){
	var theSplit = theString.indexOf(theTakeaway);
	if (theSplit != -1);{
		var theLength = theTakeaway.length;
		theString = theString.substring(0,theSplit) + theString.substring(theSplit+theLength);
		}
	return theString;
	}
	
function padRight(theString,thePad,howLong){
	while(theString.length < howLong)
		theString += thePad;
	return theString;
	}
function fileOnly(theString) {
	var theSplit = theString.lastIndexOf ('/');
	if (theSplit != -1)
		return theString.substring(theSplit+1);
	else
		return "";
	}
	
function checkForDWfile(){
	if(typeof(DWfile)=="undefined"){
		var message="Sorry, you need the DWfile library\r";
		message+="You can download it from Macromedia's website";
		alert(message);
		dreamweaver.browseDocument("http://www.macromedia.com/support/dreamweaver/extend/dwfile/");
		window.close();
		return false;
	}else
   	 return nada="";
	}
